package thread.synchronize;

/**
 * 脏读---设置Name后睡了1s 主线程开始执行 读到了旧的pwd
 *
 * @author liweimin
 * @date 2021/4/28 14:46
 * @since
 */
public class DirtyReadingTest {
    public static void main(String[] args) throws InterruptedException {
        PublicValue publicValue = new PublicValue();
        new Thread(() -> {
            publicValue.setValue("123", "213");
        }, "thread_a").start();

        Thread.sleep(200);

        publicValue.getValue();
    }

    static class PublicValue {
        private String name = "lwmzzz";
        private String pwd = "123456";

        public void getValue() {
            System.out.println(Thread.currentThread().getName() + ", get----name: " + name + " get----pwd: " + pwd);
        }

        public void setValue(String name, String pwd) {
            this.name = name;
            try {
                Thread.sleep(1000);//模拟设置name需要一段时间
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.pwd = pwd;
            System.out.println(Thread.currentThread().getName() + ", set----name: " + name + " ----pwd: " + pwd);
        }
    }
}
